{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 首先 import 必要的模块\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pregnants</th>\n",
       "      <th>Plasma_glucose_concentration</th>\n",
       "      <th>blood_pressure</th>\n",
       "      <th>Triceps_skin_fold_thickness</th>\n",
       "      <th>serum_insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>Diabetes_pedigree_function</th>\n",
       "      <th>Age</th>\n",
       "      <th>Target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.639947</td>\n",
       "      <td>0.866045</td>\n",
       "      <td>-0.031990</td>\n",
       "      <td>0.670643</td>\n",
       "      <td>-0.181541</td>\n",
       "      <td>0.166619</td>\n",
       "      <td>0.468492</td>\n",
       "      <td>1.425995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.844885</td>\n",
       "      <td>-1.205066</td>\n",
       "      <td>-0.528319</td>\n",
       "      <td>-0.012301</td>\n",
       "      <td>-0.181541</td>\n",
       "      <td>-0.852200</td>\n",
       "      <td>-0.365061</td>\n",
       "      <td>-0.190672</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.233880</td>\n",
       "      <td>2.016662</td>\n",
       "      <td>-0.693761</td>\n",
       "      <td>-0.012301</td>\n",
       "      <td>-0.181541</td>\n",
       "      <td>-1.332500</td>\n",
       "      <td>0.604397</td>\n",
       "      <td>-0.105584</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.844885</td>\n",
       "      <td>-1.073567</td>\n",
       "      <td>-0.528319</td>\n",
       "      <td>-0.695245</td>\n",
       "      <td>-0.540642</td>\n",
       "      <td>-0.633881</td>\n",
       "      <td>-0.920763</td>\n",
       "      <td>-1.041549</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.141852</td>\n",
       "      <td>0.504422</td>\n",
       "      <td>-2.679076</td>\n",
       "      <td>0.670643</td>\n",
       "      <td>0.316566</td>\n",
       "      <td>1.549303</td>\n",
       "      <td>5.484909</td>\n",
       "      <td>-0.020496</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   pregnants  Plasma_glucose_concentration  blood_pressure  \\\n",
       "0   0.639947                      0.866045       -0.031990   \n",
       "1  -0.844885                     -1.205066       -0.528319   \n",
       "2   1.233880                      2.016662       -0.693761   \n",
       "3  -0.844885                     -1.073567       -0.528319   \n",
       "4  -1.141852                      0.504422       -2.679076   \n",
       "\n",
       "   Triceps_skin_fold_thickness  serum_insulin       BMI  \\\n",
       "0                     0.670643      -0.181541  0.166619   \n",
       "1                    -0.012301      -0.181541 -0.852200   \n",
       "2                    -0.012301      -0.181541 -1.332500   \n",
       "3                    -0.695245      -0.540642 -0.633881   \n",
       "4                     0.670643       0.316566  1.549303   \n",
       "\n",
       "   Diabetes_pedigree_function       Age  Target  \n",
       "0                    0.468492  1.425995       1  \n",
       "1                   -0.365061 -0.190672       0  \n",
       "2                    0.604397 -0.105584       1  \n",
       "3                   -0.920763 -1.041549       0  \n",
       "4                    5.484909 -0.020496       1  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv(\"FE_pima-indians-diabetes.csv\")\n",
    "\n",
    "train.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#将数据分割训练数据与测试数据\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "y = train[\"Target\"]\n",
    "\n",
    "X = train.drop([\"Target\"], axis = 1)\n",
    "\n",
    "# 随机采样20%的数据构建测试样本，其余作为训练样本\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, train_size=0.8)\n",
    "\n",
    "feat_names = X_train.columns\n",
    "\n",
    "from scipy.sparse import csr_matrix\n",
    "X_train = csr_matrix(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score=nan,\n",
       "             estimator=LogisticRegression(C=1.0, class_weight=None, dual=False,\n",
       "                                          fit_intercept=True,\n",
       "                                          intercept_scaling=1, l1_ratio=None,\n",
       "                                          max_iter=100, multi_class='auto',\n",
       "                                          n_jobs=None, penalty='l2',\n",
       "                                          random_state=None, solver='liblinear',\n",
       "                                          tol=0.0001, verbose=0,\n",
       "                                          warm_start=False),\n",
       "             iid='deprecated', n_jobs=None,\n",
       "             param_grid={'C': [0.1, 1, 10, 100, 1000], 'penalty': ['l1', 'l2']},\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=True,\n",
       "             scoring='neg_log_loss', verbose=0)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "#penaltys = ['l1','l2']\n",
    "penaltys = ['l1', 'l2']\n",
    "Cs = [ 0.1, 1, 10, 100, 1000]\n",
    "tuned_parameters = dict(penalty = penaltys, C = Cs)\n",
    "\n",
    "lr_penalty= LogisticRegression(solver='liblinear')\n",
    "grid= GridSearchCV(lr_penalty, tuned_parameters,cv=5, scoring='neg_log_loss', return_train_score=True)\n",
    "grid.fit(X_train,y_train)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.4713066201391268\n",
      "{'C': 1, 'penalty': 'l2'}\n"
     ]
    }
   ],
   "source": [
    "print(-grid.best_score_)\n",
    "print(grid.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEGCAYAAAB/+QKOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAb0ElEQVR4nO3de3hddZ3v8fcnlza9xLa0BXrTFKbM2CIUqAiCHkCUikwRgYFx8DY6jOfIg8fjZaqeBwf0cWb0jHp8hnMEOSqe8VgQRSsU8YpVH7mE2kJLRWpHJTRAKDZQadpcvuePtZLu7P6S7KRZ2Wn6eT3Pbtb6rd9a+5vVZH+y7ooIzMzMytVUuwAzMxufHBBmZpbkgDAzsyQHhJmZJTkgzMwsqa7aBYyWOXPmRFNTU7XLMDM7pDz44IPPRMTc1LQJExBNTU00NzdXuwwzs0OKpN8PNM27mMzMLMkBYWZmSQ4IMzNLmjDHIMzMSnV2dtLS0kJHR0e1SxkXG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AcuBEYA7wgKT1ZLc9aQJeRnYn3q3Al/J5ziC7qhlgGbAxspuvpWwGXl5I5WYV8haE2cicSXbH2O6IeAr4KdkH+pnANyKiJyKeJLvVR695QFslC8+DY5+kxlGu26xiDgizkRnoISyDPZxlD9m9ciC7j8+Jkgb7HZwMdIygNrNR4YAwG5n1wGX5A1zmkj3W837g52QPK6qRdBTZoyl7bQX+DCCyZ3c0A9eW3BV0iaQL8+HZQFtEdI7VN2RWzgFhNjK3k91VcxPwY+BD+S6lb5LdMXUzcAPZk8ja83nupH9gvIvswVHbJD1M9uyK3mcdnA2sK/ZbMBuc7+ZqNsokTY/sSWOzybYqzoiIJ/N7+P8kHx/o4HTvMr4FfDiGfm66WWF8FpPZ6Lsjf7DLJODj+ZYFEbFH0sfInhv8h4Fmzh+E9G2Hg1WbtyDMzCzJxyDMzCzJAWFmZkkOCDMzS3JAmJlZkgPCzMyS/j9+Bjb13WekEwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot CV误差曲线\n",
    "test_means = grid.cv_results_[ 'mean_test_score' ]\n",
    "test_stds = grid.cv_results_[ 'std_test_score' ]\n",
    "train_means = grid.cv_results_[ 'mean_train_score' ]\n",
    "train_stds = grid.cv_results_[ 'std_train_score' ]\n",
    "\n",
    "\n",
    "# plot results\n",
    "n_Cs = len(Cs)\n",
    "number_penaltys = len(penaltys)\n",
    "test_scores = np.array(test_means).reshape(n_Cs,number_penaltys)\n",
    "train_scores = np.array(train_means).reshape(n_Cs,number_penaltys)\n",
    "test_stds = np.array(test_stds).reshape(n_Cs,number_penaltys)\n",
    "train_stds = np.array(train_stds).reshape(n_Cs,number_penaltys)\n",
    "\n",
    "x_axis = np.log10(Cs)\n",
    "for i, value in enumerate(penaltys):\n",
    "    #pyplot.plot(log(Cs), test_scores[i], label= 'penalty:'   + str(value))\n",
    "    plt.errorbar(x_axis, -test_scores[:,i], yerr=test_stds[:,i] ,label = penaltys[i] +' Test')\n",
    "    #plt.errorbar(x_axis, -train_scores[:,i], yerr=train_stds[:,i] ,label = penaltys[i] +' Train')\n",
    "    \n",
    "plt.legend()\n",
    "plt.xlabel( 'log(C)' )                                                                                                      \n",
    "plt.ylabel( 'logloss' )\n",
    "plt.savefig('LogisticGridSearchCV_C.png' )\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import pickle \n",
    "\n",
    "#pickle.dump(grid.best_estimator_, open(\"diabetes_lr_org.pkl\", 'wb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "lr_best = grid.best_estimator_\n",
    "y_test_pred = lr_best.predict_proba(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(154, 2)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test_pred.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.64885862, 0.35114138],\n",
       "       [0.4395221 , 0.5604779 ],\n",
       "       [0.80624181, 0.19375819],\n",
       "       [0.94367179, 0.05632821],\n",
       "       [0.4257041 , 0.5742959 ],\n",
       "       [0.67162351, 0.32837649],\n",
       "       [0.7565548 , 0.2434452 ],\n",
       "       [0.11357025, 0.88642975],\n",
       "       [0.87312634, 0.12687366],\n",
       "       [0.74338653, 0.25661347],\n",
       "       [0.39065953, 0.60934047],\n",
       "       [0.44574502, 0.55425498],\n",
       "       [0.35189113, 0.64810887],\n",
       "       [0.92413562, 0.07586438],\n",
       "       [0.23023359, 0.76976641],\n",
       "       [0.53889986, 0.46110014],\n",
       "       [0.68795089, 0.31204911],\n",
       "       [0.94525823, 0.05474177],\n",
       "       [0.7300719 , 0.2699281 ],\n",
       "       [0.9099739 , 0.0900261 ],\n",
       "       [0.93167795, 0.06832205],\n",
       "       [0.48958655, 0.51041345],\n",
       "       [0.88965677, 0.11034323],\n",
       "       [0.75255936, 0.24744064],\n",
       "       [0.77410867, 0.22589133],\n",
       "       [0.42592455, 0.57407545],\n",
       "       [0.88513399, 0.11486601],\n",
       "       [0.65843634, 0.34156366],\n",
       "       [0.05176753, 0.94823247],\n",
       "       [0.19759646, 0.80240354],\n",
       "       [0.72035997, 0.27964003],\n",
       "       [0.93045112, 0.06954888],\n",
       "       [0.72892061, 0.27107939],\n",
       "       [0.8587948 , 0.1412052 ],\n",
       "       [0.2422148 , 0.7577852 ],\n",
       "       [0.76976781, 0.23023219],\n",
       "       [0.50521351, 0.49478649],\n",
       "       [0.18551636, 0.81448364],\n",
       "       [0.89050658, 0.10949342],\n",
       "       [0.6918189 , 0.3081811 ],\n",
       "       [0.91603501, 0.08396499],\n",
       "       [0.14910602, 0.85089398],\n",
       "       [0.83853537, 0.16146463],\n",
       "       [0.93838009, 0.06161991],\n",
       "       [0.72600422, 0.27399578],\n",
       "       [0.31608146, 0.68391854],\n",
       "       [0.1456959 , 0.8543041 ],\n",
       "       [0.12271394, 0.87728606],\n",
       "       [0.64690877, 0.35309123],\n",
       "       [0.89047539, 0.10952461],\n",
       "       [0.04105533, 0.95894467],\n",
       "       [0.923298  , 0.076702  ],\n",
       "       [0.17215856, 0.82784144],\n",
       "       [0.38428123, 0.61571877],\n",
       "       [0.95876065, 0.04123935],\n",
       "       [0.75250692, 0.24749308],\n",
       "       [0.9569436 , 0.0430564 ],\n",
       "       [0.71330246, 0.28669754],\n",
       "       [0.86553997, 0.13446003],\n",
       "       [0.73258867, 0.26741133],\n",
       "       [0.37932973, 0.62067027],\n",
       "       [0.13474533, 0.86525467],\n",
       "       [0.69823057, 0.30176943],\n",
       "       [0.63602741, 0.36397259],\n",
       "       [0.71086165, 0.28913835],\n",
       "       [0.65932904, 0.34067096],\n",
       "       [0.69747737, 0.30252263],\n",
       "       [0.94690475, 0.05309525],\n",
       "       [0.70527185, 0.29472815],\n",
       "       [0.49948   , 0.50052   ],\n",
       "       [0.76001485, 0.23998515],\n",
       "       [0.93278006, 0.06721994],\n",
       "       [0.02512311, 0.97487689],\n",
       "       [0.49262979, 0.50737021],\n",
       "       [0.09934202, 0.90065798],\n",
       "       [0.5300836 , 0.4699164 ],\n",
       "       [0.92632952, 0.07367048],\n",
       "       [0.87795532, 0.12204468],\n",
       "       [0.8791105 , 0.1208895 ],\n",
       "       [0.76978819, 0.23021181],\n",
       "       [0.78426317, 0.21573683],\n",
       "       [0.96193185, 0.03806815],\n",
       "       [0.17422721, 0.82577279],\n",
       "       [0.86026012, 0.13973988],\n",
       "       [0.84174123, 0.15825877],\n",
       "       [0.74099566, 0.25900434],\n",
       "       [0.86049723, 0.13950277],\n",
       "       [0.68144103, 0.31855897],\n",
       "       [0.5094156 , 0.4905844 ],\n",
       "       [0.43668033, 0.56331967],\n",
       "       [0.56638449, 0.43361551],\n",
       "       [0.72093393, 0.27906607],\n",
       "       [0.38558763, 0.61441237],\n",
       "       [0.24411954, 0.75588046],\n",
       "       [0.8452252 , 0.1547748 ],\n",
       "       [0.98112783, 0.01887217],\n",
       "       [0.93585381, 0.06414619],\n",
       "       [0.54352984, 0.45647016],\n",
       "       [0.3134443 , 0.6865557 ],\n",
       "       [0.97085542, 0.02914458],\n",
       "       [0.43044783, 0.56955217],\n",
       "       [0.96102839, 0.03897161],\n",
       "       [0.90338405, 0.09661595],\n",
       "       [0.84510759, 0.15489241],\n",
       "       [0.542494  , 0.457506  ],\n",
       "       [0.68095821, 0.31904179],\n",
       "       [0.93547721, 0.06452279],\n",
       "       [0.90074454, 0.09925546],\n",
       "       [0.86416859, 0.13583141],\n",
       "       [0.77072698, 0.22927302],\n",
       "       [0.91653299, 0.08346701],\n",
       "       [0.68254012, 0.31745988],\n",
       "       [0.93174488, 0.06825512],\n",
       "       [0.73117248, 0.26882752],\n",
       "       [0.74724344, 0.25275656],\n",
       "       [0.79616936, 0.20383064],\n",
       "       [0.31993329, 0.68006671],\n",
       "       [0.49500765, 0.50499235],\n",
       "       [0.74923789, 0.25076211],\n",
       "       [0.32019752, 0.67980248],\n",
       "       [0.34088657, 0.65911343],\n",
       "       [0.83724397, 0.16275603],\n",
       "       [0.57998777, 0.42001223],\n",
       "       [0.76939124, 0.23060876],\n",
       "       [0.62030039, 0.37969961],\n",
       "       [0.87600288, 0.12399712],\n",
       "       [0.84145662, 0.15854338],\n",
       "       [0.76028474, 0.23971526],\n",
       "       [0.73028538, 0.26971462],\n",
       "       [0.75727094, 0.24272906],\n",
       "       [0.78196222, 0.21803778],\n",
       "       [0.90534374, 0.09465626],\n",
       "       [0.93439286, 0.06560714],\n",
       "       [0.3404463 , 0.6595537 ],\n",
       "       [0.79304021, 0.20695979],\n",
       "       [0.21603169, 0.78396831],\n",
       "       [0.77804012, 0.22195988],\n",
       "       [0.86994304, 0.13005696],\n",
       "       [0.80684387, 0.19315613],\n",
       "       [0.96834762, 0.03165238],\n",
       "       [0.61841429, 0.38158571],\n",
       "       [0.74510056, 0.25489944],\n",
       "       [0.9887799 , 0.0112201 ],\n",
       "       [0.67526605, 0.32473395],\n",
       "       [0.84484511, 0.15515489],\n",
       "       [0.6458128 , 0.3541872 ],\n",
       "       [0.74949438, 0.25050562],\n",
       "       [0.94005847, 0.05994153],\n",
       "       [0.7328602 , 0.2671398 ],\n",
       "       [0.74005249, 0.25994751],\n",
       "       [0.48039843, 0.51960157],\n",
       "       [0.56064727, 0.43935273],\n",
       "       [0.76364023, 0.23635977],\n",
       "       [0.2324912 , 0.7675088 ]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#生成提交结果\n",
    "out_df = pd.DataFrame(y_test_pred)\n",
    "\n",
    "columns = np.empty(2, dtype=object)\n",
    "for i in range(2):\n",
    "    columns[i] = 'Class_' + str(i+1)\n",
    "\n",
    "out_df.columns = columns\n",
    "\n",
    "out_df.to_csv(\"LR_org_Test_result.csv\", index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
